//
//  LZFinancialPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/22.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZFinancialPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "MJExtension.h"

#import "LZFinancialPayedModel.h"
#import "LZOrderModel.h"
#import "LZFinancialSettlementAccountModel.h"

extern NSString *merchant;
static NSString *const kFinancialSummaryURL = @"/settlement/info";    //财务摘要
static NSString *const kFinancialHaveBeenPayedURL = @"/settlement/listSettlementInfo";  //已经结账列表
static NSString *const kFinancialOneMonthRecordURL = @"/settlement/settlementDetail"; //已经结账 月列表
static NSString *const kFinancialSettlementInfoURL = @"/settlement/settlementBankInfo"; //账户信息

@implementation LZFinancialPresenter

#pragma mark -
#pragma mark 获取财务摘要
/**
 获取 财务摘要
 
 @param sellerId 商户id
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getFinanicalSummaryWithSellerID:(NSString *)sellerId resultSuccess:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kFinancialSummaryURL);
    NSDictionary *parameter = @{@"sellerId" : sellerId};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameter success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            SAFE_BLOCK_INVOKE(successBlock, resultData[@"data"]);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
}

#pragma mark -
#pragma mark 获取已经结账列表
/**
 获取 已经结账 列表
 
 @param sellerId 商户 id
 @param cPage 当前页数
 @param limit 每页数量
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getFinanicalHaveBeenPayedListWithSellerID:(NSString *)sellerId CurrentPage:(NSInteger)cPage Limit:(NSInteger)limit resultSuccess:(void (^)(NSArray *, NSInteger, NSTimeInterval))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kFinancialHaveBeenPayedURL);
    NSDictionary *parameter = @{@"sellerId" : sellerId,
                                @"cpage" : [NSString stringWithFormat:@"%ld",cPage],
                                @"limit" : [NSString stringWithFormat:@"%ld",limit]};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameter success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            
            NSInteger totalPage = [resultData[@"data"][@"totalCount"] integerValue];
            NSTimeInterval timeStampTemp = [resultData[@"timestamp"] doubleValue];
            NSArray *arr = resultData[@"data"][@"list"];
            NSMutableArray *modelArr = @[].mutableCopy;
            for (int i = 0; i < arr.count; i ++) {
                LZFinancialPayedModel *m = [LZFinancialPayedModel mj_objectWithKeyValues:arr[i]];
                [modelArr addObject:m];
            }
            
            if (successBlock) {
                successBlock([modelArr copy], totalPage,timeStampTemp);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
    
}

#pragma mark -
#pragma mark 获取已经结账 一月的数据
/**
 获取 已经结账一月数据 列表
 
 @param payedId 结账id
 @param cPage 当前页数
 @param limit 每页数量
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getFinanicalOneMonthRecordWithPayedID:(NSString *)payedId CurrentPage:(NSInteger)cPage Limit:(NSInteger)limit resultSuccess:(void (^)(NSArray *, NSInteger))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kFinancialOneMonthRecordURL);
    NSDictionary *parameter = @{@"payCountId" : payedId,
                                @"cpage" : [NSNumber numberWithInteger:cPage],
                                @"limit" : [NSNumber numberWithInteger:limit]};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameter success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            
            NSInteger totalPage = [resultData[@"data"][@"totalCount"] integerValue];
            NSArray *arr = resultData[@"data"][@"list"];
            NSMutableArray *modelArr = @[].mutableCopy;
            for (int i = 0; i < arr.count; i ++) {
                LZOrderModel *m = [LZOrderModel mj_objectWithKeyValues:arr[i]];
                [modelArr addObject:m];
            }
            
            if (successBlock) {
                successBlock(modelArr, totalPage);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];

}

#pragma mark -
#pragma  mark 获取账户信息

/**
 获取账户信息
 
 @param idStr 月结列表id
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getFinanicalSettlementAccountInfoWithID:(NSString *)idStr ResultSuccess:(void (^)(LZFinancialSettlementAccountModel *))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kFinancialSettlementInfoURL);
    NSDictionary *dic = @{@"id" : idStr};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            LZFinancialSettlementAccountModel *m = [LZFinancialSettlementAccountModel mj_objectWithKeyValues:resultData[@"data"]];
            if (successBlock) {
                successBlock(m);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
}



@end
